package com.ruoyi.order.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.employee.domain.EmployeeStatic;
import com.ruoyi.employee.service.IEmployeeStaticService;
import com.ruoyi.market.domain.ConfigEmployee;
import com.ruoyi.market.service.IConfigEmployeeService;
import com.ruoyi.order.domain.OrderCashEmployee;
import com.ruoyi.order.mapper.OrderCashEmployeeMapper;
import com.ruoyi.order.service.IOrderCashEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.List;

/**
 * 店员提现订单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class OrderCashEmployeeServiceImpl implements IOrderCashEmployeeService
{
    @Autowired
    private OrderCashEmployeeMapper orderCashEmployeeMapper;

    @Autowired
    private IEmployeeStaticService employeeStaticService;

    @Autowired
    private IConfigEmployeeService configEmployeeService;

    @Autowired
    protected Validator validator;

    /**
     * 根据条件分页查询店员提现订单列表
     * 
     * @param orderCashEmployee 店员提现订单信息
     * @return 店员提现订单信息集合信息
     */
    @Override
    public List<OrderCashEmployee> selectOrderCashEmployeeList(OrderCashEmployee orderCashEmployee)
    {
        return orderCashEmployeeMapper.selectOrderCashEmployeeList(orderCashEmployee);
    }

    /**
     * 通过店员提现订单编号查询店员提现订单
     * 
     * @param orderNo 店员提现订单编号
     * @return 店员提现订单对象信息
     */
    @Override
    public OrderCashEmployee selectOrderCashEmployeeByOrderNo(String orderNo)
    {
        return orderCashEmployeeMapper.selectOrderCashEmployeeByOrderNo(orderNo);
    }

    /**
     * 查询一周累计总数 次数
     *
     * @param orderCashEmployee 店员提现信息
     * @return 店员提现对象信息
     */
    @Override
    public int selectOrderEmployeeCountTotalInterval(OrderCashEmployee orderCashEmployee)
    {
        return orderCashEmployeeMapper.selectOrderEmployeeCountTotalInterval(orderCashEmployee);
    }

    /**
     * 新增保存店员提现订单信息
     * 
     * @param orderCashEmployee 店员提现订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrderCashEmployee(OrderCashEmployee orderCashEmployee)
    {
        // 新增店员提现订单信息
        Long userId = SecurityUtils.getUserId();
        orderCashEmployee.setUserId(userId);
        orderCashEmployee.setStatus(UserConstants.STATUS_AUDIT);
        EmployeeStatic employeeStatic = employeeStaticService.selectUserById(userId);
        if(employeeStatic.getBalance().compareTo(orderCashEmployee.getAmount()) == -1){
            throw new ServiceException("提现失败：可用余额不足");
        }
        ConfigEmployee configEmployee = configEmployeeService.selectConfigEmployeeByShopId(orderCashEmployee.getShopId());
        if(configEmployee != null){
            if(configEmployee.getAmountMin() != null){
                if(orderCashEmployee.getAmount().compareTo(configEmployee.getAmountMin()) < 0){
                    throw new ServiceException("操作失败：提现金额最小为:" + configEmployee.getAmountMin() + "元");
                }
            }
            if(configEmployee.getAmountMax() != null){
                if(orderCashEmployee.getAmount().compareTo(configEmployee.getAmountMax()) > 0){
                    throw new ServiceException("操作失败：提现金额最大为:" + configEmployee.getAmountMax() + "元");
                }
            }
            if(configEmployee.getCashTimes() > 0){
                int count = orderCashEmployeeMapper.selectOrderEmployeeCountTotalInterval(orderCashEmployee);
                if(configEmployee.getCashTimes() < count){
                    throw new ServiceException("操作失败：已达每周最大提现次数");
                }
            }
        }
        String orderNo = MyUtils.getOrderNoByTimeAndId(userId);
        orderCashEmployee.setOrderNo(orderNo);
        // 更新店员余额 减去
        employeeStatic.setBalance(employeeStatic.getBalance().subtract(orderCashEmployee.getAmount()));
        employeeStaticService.updateUser(employeeStatic);
        int rows = orderCashEmployeeMapper.insertOrderCashEmployee(orderCashEmployee);
        return rows;
    }

    /**
     * 修改保存店员提现订单信息
     * 
     * @param orderCashEmployee 店员提现订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrderCashEmployee(OrderCashEmployee orderCashEmployee)
    {
        // 驳回
        if(orderCashEmployee.getStatus().equals(UserConstants.STATUS_REJECT)){
            EmployeeStatic employeeStatic = employeeStaticService.selectUserById(orderCashEmployee.getUserId());
            // 更新余额 加
            employeeStatic.setBalance(employeeStatic.getBalance().add(orderCashEmployee.getAmount()));
            employeeStaticService.updateUser(employeeStatic);
        }
        // 通过
        if(orderCashEmployee.getStatus().equals(UserConstants.STATUS_APPROVE)){
            EmployeeStatic employeeStatic = employeeStaticService.selectUserById(orderCashEmployee.getUserId());
            // 用户已提佣金 加
            employeeStatic.setMoneyGet(employeeStatic.getMoneyGet().add(orderCashEmployee.getAmount()));
            employeeStaticService.updateUser(employeeStatic);
        }
        return orderCashEmployeeMapper.updateOrderCashEmployee(orderCashEmployee);
    }

}
